﻿$(function () {

    $.fn.InitItemContainer = InitItemContainer;

    //initializing current album item container
    $("div[id*=itemContainer]").InitItemContainer();

    $("#addphoto,#addalbum").live("click", function () {
        var position = $(this).offset();
        message_box.show_load(position.left, position.top, false)
        $.get($(this).attr("href"),
                        function (data) {
                            message_box.replace_content(data);

                        });

        return false;
    })


    //initializing expanding
    $(".album:has([id^=itemContainer])>.AlbumIcon").live('click',
        function (event) {
            var itemContainer = $(this).siblings("[id*=itemContainer]");


            if (itemContainer.is("DIV:empty")) {
                var albumId = itemContainer.attr('id').match(/\d+/)[0];
                itemContainer.hide().append("<div id='Loading'><img  alt=''  src='/Content/loading.gif'></div>").slideDown("fast");
                $.ajax({
                    url: albumId,
                    type: "GET",
                    dataType: "html",
                    success: function (response) {
                        itemContainer.empty().append(response);
                        itemContainer.InitItemContainer();
                    }
                });
                $(this).css('background-image', 'url("/Content/minus.jpg")');
            }
            else {
                if (itemContainer.is("DIV:hidden")) {
                    $(this).css('background-image', 'url("/Content/minus.jpg")');
                    itemContainer.slideDown('fast');
                }
                else {
                    $(this).css('background-image', "url('/Content/plus.jpg')");
                    itemContainer.slideUp('fast');

                }

            }
            return false;

        }
    )


    //function for initializing item container. This object is Jquery wraped item container
    function InitItemContainer() {
        if (!this.is("DIV[id*=itemContainer]"))
            return;
        var itemContainer = $(this);
        itemContainer.find(".album:has([id*=itemContainer])>.AlbumIcon").css('background-image', "url('/Content/plus.jpg')");
        var pagers = itemContainer.children().filter(".pager"),
            firstPager = pagers.first(),
            container = itemContainer.children().filter(".container");
        pagers.Pager({
            TotalPages: firstPager.children().length,
            StartPage: container.children(),
            StartPageIndex: firstPager.children().index(firstPager.find(".selected"))+1,
            FormatAjaxResult: function (html) {
                return $(html).find(".Page");
            },
            UrlWildcard: function () {
                var urlTemplate = firstPager.children().attr("href");
                return function (ind) {
                    return urlTemplate.replace(/(page=)\d+/, "$1" + ind);
                }
            },
            NewIndex: function (event) {
                if (event.target.nodeName === "A") {
                    event.stopPropagation();
                    return $(event.target).text();
                }
                else return -1;
            },
            FormatPager: function (index) {
                $(this).find(".selected").removeClass("selected");
                
                $(this).children().eq(index-1).addClass("selected");
            },
            Container: container
        });
    }
})
